<?php

namespace App\Task\Stat;

use App\Model\PayOrder;
use App\Model\Stat\StatDailyPayInfo;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 支付日报
 * @Crontab(name="StatDailyPayInfoTask", rule="5 *\/1 * * *", callback="execute", memo="每日充值统计", singleton=false)
 */
class StatDailyPayInfoTask
{
    public function execute()
    {
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        //一天前5分钟内
        if (time() - strtotime(date('Y-m-d')) < 600) {
            $end = date('Y-m-d');
            $start = date('Y-m-d', strtotime('-1 day'));
        } else {
            $end = date('Y-m-d', strtotime('+1 day'));
            $start = date('Y-m-d');
        }
        $model = StatDailyPayInfo::where(['dt' => $start])->first();
        if (!$model) $model = new StatDailyPayInfo();
        $model->dt = $start;
        $model->pay_order_num = PayOrder::getRechargeNum('pay_order_num', $start, $end);
        $model->pay_user_num = PayOrder::getRechargeNum('pay_user_num', $start, $end);
        $model->pay_rmb_num = PayOrder::getRechargeNum('pay_rmb_num', $start, $end);
        $model->wechat_order_num = PayOrder::getRechargeNum('wechat_order_num', $start, $end);
        $model->wechat_rmb_num = PayOrder::getRechargeNum('wechat_rmb_num', $start, $end);
        $model->alipay_order_num = PayOrder::getRechargeNum('alipay_order_num', $start, $end);
        $model->alipay_rmb_num = PayOrder::getRechargeNum('alipay_rmb_num', $start, $end);
        $model->fail_order_num = PayOrder::getRechargeNum('fail_num', $start, $end);
        $model->fail_user_num = PayOrder::getRechargeNum('fail_user_num', $start, $end);
        $payList = $this->getRechargeNumInfo($start, $end);
        $model->order_num_info = json_encode($this->getOrderNum($payList));
        $model->user_num_info = json_encode($this->getUserNum($payList));
        $model->invite_rmb_num = PayOrder::getInviteData($start, $end, true, 'rmb');
        $model->uninvite_rmb_num = PayOrder::getInviteData($start, $end, false, 'rmb');
        $model->invite_user_num = PayOrder::getInviteData($start, $end, true, 'user');
        $model->uninvite_user_num = PayOrder::getInviteData($start, $end, false, 'user');
        $model->saveOrFail();
    }

    function getRechargeNumInfo($start = '', $end = '')
    {
        $where = [['status', PayOrder::STATUS_OK]];
        if ($start) $where[] = ['finished_at', '>=', (string)$start];
        if ($end) $where[] = ['finished_at', '<', (string)$end];
        $data = PayOrder::where($where)->select(['total_amount', 'user_id'])->get()->toArray();
        return $data;
    }

    function getOrderNum($orderList = [])
    {
        $info = [];
        foreach ($orderList as $item) {
            $key = (string)($item['total_amount'] / 100);
            if (!isset($info[$key])) $info[$key] = 0;
            $info[$key]++;
        }
        return $info;
    }

    function getUserNum($orderList = [])
    {
        $info = [];
        foreach ($orderList as $item) {
            $key = (string)($item['total_amount'] / 100);
            if (!isset($info[$key])) $info[$key] = [];
            $info[$key][] = $item['user_id'];
        }
        foreach ($info as $key => &$item) {
            $item = count(array_unique($item));
        }
        return $info;
    }
}
